package 尚硅谷多线程1;
/*
* 线程的优先级：
* 1.
* MAX_PRIORITY :10
* MIN_PRIORITY :1
* NORM_PRIORITY :5 默认优先级
* 2.如何获取和设置当前线程的优先级:
* getPriority():获取线程的优先级
* setPriority(int p) 设置线程的优先级 说明：高优先级的线程要抢占低优先级的CPU的执行权，但是
*                     只是从概率上讲，高优先级的线程高概率的情况下被执行。并不意味着只有当高优先级的线程执行完以后，低优先级的线程才执行。
*
* */
    class hello1Thread extends Thread{
        @Override
        public void run(){

            for (int i = 0; i < 100; i++) {

                if(i%2==0){
                    System.out.println(getPriority()+":"+getName()+":"+i);
                }
            }
        }
        public hello1Thread(String name){
            super(name);
        }
    }

    public class 线程的优先级 {
        public static void main(String[] args) {


            尚硅谷多线程1.hello1Thread h2= new 尚硅谷多线程1.hello1Thread("子");
            h2.setPriority(Thread.MAX_PRIORITY);//设置优先级
            h2.start();
            Thread.currentThread().setName("主线程");
            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);//设置优先级
            for (int i = 1; i < 100; i++) {
                if (i % 2 == 0) {
                    System.out.println(Thread.currentThread().getName()+":"+ Thread.currentThread().getPriority()+ ":"+i);
                }
                if (i%20==0){
                    try {
                        h2.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }


